package com.engc.common.fileupload;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import com.engc.common.utils.ReadProperties;

/**
 * FileUpload Manager 
 * @author yangchundu
 *
 */

public class FileUploadManager {
	
	private String ip = new ReadProperties("config/fileserver.properties").getValue("ServerIp");
	private String port = new ReadProperties("config/fileserver.properties").getValue("ServerPort");
	private String filepath = new ReadProperties("config/fileserver.properties").getValue("FilePath");
	private String imagepath = new ReadProperties("config/fileserver.properties").getValue("ImagePath");
	private String videopath = new ReadProperties("config/fileserver.properties").getValue("VideoPath");
	
	
	/**
	 * 文件上传到服务器,返回访问路径
	 * @param type
	 * @param file_name
	 * @return
	 */
	public String uploadFile(int type, File file_name) {
	    URL url = null;
	    HttpURLConnection hc = null;
	    byte[] raw = null;
	    FileInputStream in = null;
	    File file = null;
	    try {
	        file = file_name;
	        long file_size = 0;
	        if (file.exists()) {
	            file_size = file.length();
	        }
	        in = new FileInputStream(file_name);
	        raw = new byte[Integer.parseInt(String.valueOf(file_size))];
	 
	        int i = 0;
	        int index = 0;
	        while ((i = in.read()) != -1) {
	            raw[index++] = (byte) i;
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	        return null;
	    } finally {
	        try {
	            if (in != null) {
	                in.close();
	            }
	        } catch (Exception e) {
	        }
	    }
	    
	    
	    //判断上传文件类型，存取文件服务器不同的文件夹中
	    String remoe_folder="";
	    switch(type){
	       case(1):remoe_folder="/"+filepath+"/";break;
	       case(2):remoe_folder="/"+imagepath+"/";break;
	       case(3):remoe_folder="/"+videopath+"/";break;
	    }
	    
	    //文件名称生成规则 date.time+filename;
	    String filename=file.getName();
	    String filetype=getExtensionName(filename);
	    remoe_folder=remoe_folder+filename;
	       
	    String remoe_up_http_url = "";
	    try {
	        remoe_up_http_url = "http://"+ip+":"+port+"/upload?Command=FileUpload&Type="
	                + filetype+ "&fileName="+ URLEncoder.encode(remoe_folder, "UTF-8");
	        url = new URL(remoe_up_http_url);
	        
	        hc = (HttpURLConnection) url.openConnection();
	        hc.setRequestMethod("POST");
	        hc.setDoOutput(true);
	        hc.setDoInput(true);
	        hc.getOutputStream().write(raw, 0, raw.length);
	        
	        hc.getOutputStream().close();
	        int i=hc.getResponseCode();
	        System.out.println(i);
	        
            String path= remoe_folder;
            return path;
	    } catch (Exception e) {
	        e.printStackTrace();
	        return null;
	    } finally {
	        try {
	            hc.getOutputStream().close();
	        } catch (Exception e) {
	 
	        }
	        hc.disconnect();
	    }
	}
	
	/*
	 * Java文件操作 获取文件扩展名
	 * 
	 * Created on: 2011-8-2 Author: blueeagle
	 */
	public static String getExtensionName(String filename) {
		if ((filename != null) && (filename.length() > 0)) {
			int dot = filename.lastIndexOf('.');
			if ((dot > -1) && (dot < (filename.length() - 1))) {
				return filename.substring(dot + 1);
			}
		}
		return filename;
	}
//	public static void main(String[] args) {
//		FileUploadManager fu=new FileUploadManager();
//		String a=fu.uploadFile(2, "E:/1.jpg");
//		System.out.println(a);
//	}
}
